package com.example.carapi.mapper;


import com.example.carapi.bean.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

// 自动实现增删改查类
@Mapper
public interface UserMapper {
    // @Param 绑定SQL参数
    @Select("select *\n" +
            "from user\n" +
            "where id=#{id}")
    User getOne(@Param("id") Long id);

    @Select("select *\n" +
            "from user")
    List<User> getAll();

    // 数据库已经设置了主键自增，不需要手动插入id了
    @Insert("insert\n" +
            "into user(username, password, role)\n" +
            "values(#{username}, #{password}, #{role})")
    void insert(User user);

    @Insert("insert\n" +
            "into user(id, username, password, role)\n" +
            "values(#{id}, #{username}, #{password}, #{role})")
    void insertWithId(User user);

    @Update("update user\n" +
            "set username=#{username}, password=#{password}, role=#{role}\n" +
            "where id=#{id}")
    void update(User user);

    @Delete("delete\n" +
            "from user\n" +
            "where id=#{id}")
    void delete(@Param("id") Long id);
}
